package com.neusoft.databus.common.pool;

import org.apache.commons.pool2.BaseKeyedPooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;

/**
 * for simply used by concrete class
 *
 * @author sunchf
 * @date 2018年12月13日 下午1:58:19
 */
public abstract class AbstractPoolFactory<K, V> extends BaseKeyedPooledObjectFactory<K, V> {

    @Override
    public PooledObject<V> wrap(V value) {
        return new DefaultPooledObject<>(value);
    }

    @Override
    public void destroyObject(K key, PooledObject<V> p) throws Exception {
        final V v = p.getObject();
        this.destroy(v);
    }

    /**
     * you must implement destroy logic. this method may be called by pool or
     * manually.
     *
     * @param v
     */
    public abstract void destroy(V v);

}
